package com.ideal.frame.node.netty.client;

import com.ideal.frame.node.netty.NettyClient;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

/**
 * Created by haoxy on 2018/10/17.
 * E-mail:hxyHelloWorld@163.com
 * github:https://github.com/haoxiaoyong1014
 */
@Component
@ConditionalOnBean(NettyClient.class)
public class NettyClientHandleInitializer extends ChannelInitializer<Channel> {

    @Value("${netty.client.write-frequency:5}")
    private int writeIdleTimeSecond;

    @Autowired
    NettyClientHandle nettyClientHandle;

    @Override
    protected void initChannel(Channel channel) throws Exception {
        channel.pipeline()
                //10 秒没发送消息 将IdleStateHandler 添加到 ChannelPipeline 中
                .addLast(new IdleStateHandler(0, writeIdleTimeSecond, 0))
                .addLast(new NettyClientMsgEncode())
                .addLast(nettyClientHandle);
    }
}
